package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.dto.StudentAddDto;
import com.itheima.dto.StudentQueryDto;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import com.itheima.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;

    @Override
    public PageBean query(StudentQueryDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<StudentVo> query = (Page<StudentVo>) studentMapper.query(dto);
        long total = query.getTotal();
        List<StudentVo> result = query.getResult();
        PageBean pageBean = new PageBean(total,result);
        return pageBean;
    }

    @Override
    public void add(StudentAddDto student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    @Override
    public void deleteid(Integer id) {
        studentMapper.deleteid(id);
    }

    @Override
    public Student selectid(Integer id) {
        Student selectid = studentMapper.selectid(id);
        return selectid;
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.violation(id, score);
    }


}
